<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\AdminRechargeController;
use App\Http\Controllers\Admin\AdminBankDetailController;
use App\Http\Controllers\Admin\LandingCompanyController;
use App\Http\Controllers\Admin\TaskServiceTypeController;
use App\Http\Controllers\Admin\TenantController;
use App\Http\Controllers\Admin\InvoiceController as AdminInvoiceController;
use App\Http\Controllers\Admin\FreelancerController as AdminFreelancerController;

/*
|--------------------------------------------------------------------------
| Admin Routes
|--------------------------------------------------------------------------
|
| Here is where you can register admin routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return response()->json([
        'message' => 'Admin API',
        'tenant_type' => request()->attributes->get('tenant_type')
    ]);
});

// 管理端API路由组
Route::prefix('api')->group(function () {
    // 管理端认证路由
    Route::prefix('admin')->group(function () {
        Route::get('/', function () {
            return response()->json([
                'message' => 'Admin API',
            ]);
        });

        // 公共路由（无需认证）
        Route::post('login', [AuthController::class, 'login']);

        // 需要认证的路由
        Route::middleware(['auth:admin'])->group(function () {
            Route::get('me', [AuthController::class, 'me']);
            Route::get('codes', [AuthController::class, 'getAccessCodes']);
            Route::post('change-password', [AuthController::class, 'changePassword']);
            Route::post('logout', [AuthController::class, 'logout']);

            // 用户管理路由
            Route::prefix('users')->group(function () {
                // TODO: 实现用户管理路由
            });

            // 租户管理路由
            Route::prefix('tenants')->group(function () {
                Route::get('/', [TenantController::class, 'index']);
                Route::post('/', [TenantController::class, 'store']);
                Route::get('/{tenant}', [TenantController::class, 'show']);
                Route::get('/{tenant}/tasks', [TenantController::class, 'tasks']);
                Route::get('/{tenant}/tasks/{task}/applications', [TenantController::class, 'taskApplications']);
                Route::post('/{tenant}/tasks/{task}/applications/manual', [TenantController::class, 'manualTaskApplication']);
                Route::get(
                    '/{tenant}/tasks/{task}/applications/{application}/contract',
                    [TenantController::class, 'previewTaskApplicationContract']
                );
                Route::put('/{tenant}', [TenantController::class, 'update']);
                Route::put('/{tenant}/status', [TenantController::class, 'updateStatus']);
            });

            // 落地公司管理路由
            Route::prefix('landing-companies')->group(function () {
                Route::get('/', [LandingCompanyController::class, 'index']);
                Route::post('/', [LandingCompanyController::class, 'store']);
                Route::put('/{landingCompany}', [LandingCompanyController::class, 'update']);
            });

            // 任务服务类型管理
            Route::prefix('task-service-types')->group(function () {
                Route::get('/', [TaskServiceTypeController::class, 'index']);
                Route::post('/', [TaskServiceTypeController::class, 'store']);
                Route::put('/{taskServiceType}', [TaskServiceTypeController::class, 'update']);
                Route::delete('/{taskServiceType}', [TaskServiceTypeController::class, 'destroy']);
            });

            // 报酬发放管理路由
            Route::prefix('payments')->group(function () {
                // TODO: 实现报酬发放管理路由
            });

            Route::prefix('freelancers')->group(function () {
                Route::get('/', [AdminFreelancerController::class, 'index']);
                Route::post('/', [AdminFreelancerController::class, 'store']);
                Route::post('/batch-action', [AdminFreelancerController::class, 'batchAction']);
                Route::get('/{freelancer}', [AdminFreelancerController::class, 'show']);
                Route::put('/{freelancer}', [AdminFreelancerController::class, 'update']);
            });

            // 开票管理路由
            Route::prefix('invoices')->group(function () {
                Route::get('/', [AdminInvoiceController::class, 'index']);
                Route::get('/status-options', [AdminInvoiceController::class, 'statusOptions']);
                Route::post('/{invoice}/accept', [AdminInvoiceController::class, 'accept']);
                Route::post('/{invoice}/reject', [AdminInvoiceController::class, 'reject']);
                Route::post('/{invoice}/upload', [AdminInvoiceController::class, 'upload']);
                Route::post('/{invoice}/void', [AdminInvoiceController::class, 'void']);
                Route::get('/{invoice}/preview', [AdminInvoiceController::class, 'preview']);
            });

            // 充值管理路由
            Route::prefix('recharges')->group(function () {
                Route::get('/', [AdminRechargeController::class, 'index']); // 充值列表
                Route::get('/{id}', [AdminRechargeController::class, 'show']); // 充值详情
                Route::post('/{id}/confirm-match', [AdminRechargeController::class, 'confirmMatch']); // 确认匹配
                Route::delete('/{id}/cancel-match', [AdminRechargeController::class, 'cancelMatch']); // 取消匹配
                Route::post('/batch-confirm', [AdminRechargeController::class, 'batchConfirm']); // 批量确认
                Route::post('/run-matching', [AdminRechargeController::class, 'runMatching']); // 手动执行匹配算法
            });

            // 银行明细管理路由
            Route::prefix('bank-details')->group(function () {
                Route::get('/', [AdminBankDetailController::class, 'index']); // 银行明细列表
                Route::get('/{id}', [AdminBankDetailController::class, 'show']); // 明细详情
                Route::get('/{id}/match-suggestions', [AdminBankDetailController::class, 'matchSuggestions']); // 匹配建议
                Route::put('/{id}/status', [AdminBankDetailController::class, 'updateStatus']); // 更新明细状态
                Route::post('/batch-update-status', [AdminBankDetailController::class, 'batchUpdateStatus']); // 批量更新状态
                Route::post('/sync', [AdminBankDetailController::class, 'sync']); // 触发银行明细同步
            });
        });
    });
});
